package edu.whu.demo.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.whu.demo.domain.Message;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import edu.whu.demo.domain.MessageDTO;
import org.apache.ibatis.annotations.*;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 
 * @since 2022-12-15
 */
@Mapper
public interface MessageDao extends BaseMapper<Message> {

    //从mysql5.7开始，子查询的排序已经变为无效了。
    @Select("select m.*, MAX(time) " +
            "FROM (select * from message " +
            "where to_uid=#{toUid} and type=#{type} and DATE_FORMAT(time,'%Y-%m-%d %H:%i:%S') < DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%S') " +
            "ORDER BY time DESC limit 10000) as m " +
            "GROUP BY m.activity_id")
    @Results({
            @Result(property = "fromUid", column = "from_uid"),
            @Result(
                    property = "userName", column = "from_uid",
                    javaType = String.class,
                    one = @One(select = "edu.whu.demo.dao.UserDao.findNameByUid")
            ),
            @Result(property = "activityId", column = "activity_id"),
            @Result(
                    property = "activityName", column = "activity_id",
                    javaType = String.class,
                    one = @One(select = "edu.whu.demo.dao.ActivityDao.findNameById")
            )
            ,
            @Result(
                    property = "pic", column = "activity_id",
                    javaType = String.class,
                    one = @One(select = "edu.whu.demo.dao.ActivityDao.findPic1ById")
            )
            ,
            @Result(
                    property = "num", column = "{activityId=activity_id, type=type, toUid=to_uid}",
                    javaType = Integer.class,
                    one = @One(select = "edu.whu.demo.dao.MessageDao.getUnreadNum")
            )
    })
    IPage<MessageDTO> getOuterMessage(Page<MessageDTO> page,
                                      String type,
                                      Long toUid);

    @Select("select * from message ${ew.customSqlSegment} ORDER BY time DESC")
    @Results({
            @Result(property = "fromUid", column = "from_uid"),
            @Result(
                    property = "userName", column = "from_uid",
                    javaType = String.class,
                    one = @One(select = "edu.whu.demo.dao.UserDao.findNameByUid")
            ),
            @Result(property = "activityId", column = "activity_id"),
            @Result(
                    property = "activityName", column = "activity_id",
                    javaType = String.class,
                    one = @One(select = "edu.whu.demo.dao.ActivityDao.findNameById")
            ),
            @Result(
                    property = "pic", column = "activity_id",
                    javaType = String.class,
                    one = @One(select = "edu.whu.demo.dao.ActivityDao.findPic1ById")
            )
    })
    IPage<MessageDTO> getMessage(Page<MessageDTO> page, @Param(Constants.WRAPPER) QueryWrapper<MessageDTO> qw);

    @Select("select count(*) from message " +
            "where activity_id=#{activityId} and type=#{type} and to_uid=#{toUid} and is_read=0")
    Integer getUnreadNum(@Param("activityId") Long activityId,
                         @Param("type") String type,
                         @Param("toUid") Long toUid);


    @Delete(
            "delete from message " +
                    " where from_uid = #{scheduleDetailId}"
    )
    public void deleteMessageByScheduleDetail(Long scheduleDetailId);

}
